package com.oocl.chat.webcontroller;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import org.jdom.Element;
import org.jdom.output.XMLOutputter;
/**
 * 使用4dom生成HTTP
 * @author Evan
 *
 */
public class Dom {
	public void writeNotFoundPage(OutputStream out) throws IOException {

		XMLOutputter xmlOut = new XMLOutputter();
		Element html = new Element("html");
		Element body = new Element("body");
		Element notFound = new Element("h3");
		notFound.setText("404 Not Found !");
		body.addContent(notFound);
		html.addContent(body);
		xmlOut.output(html, out);
	}

	public void writeHome(OutputStream out, List<String> userlist)
			throws IOException {

		XMLOutputter xmlOut = new XMLOutputter();
		Element html = new Element("html");
		Element body = new Element("body");
		Element buttone1 = new Element("button");
		Element buttone2 = new Element("button");
		Element a1 = new Element("a");
		Element a2 = new Element("a");
		html.addContent(body);
		a1.setText("OPEN");
		a1.setAttribute("href", "/openServer");
		a2.setText("CLOSE");
		a2.setAttribute("href", "/closeServer");
		buttone1.addContent(a1);
		buttone2.addContent(a2);
		body.addContent(buttone1);
		body.addContent(buttone2);
		body.addContent(new Element("br"));
		
		Element ul = new Element("ul");
		Element li = null;
		if (userlist != null) {

			for (String string : userlist) {
				li = new Element("li");
				li.setText(string);
				ul.addContent(li);
			}
		}
		if (ul.getChildren().size() == 0) {
			Element h2 = new Element("h2");
			h2.setText("no user");
			body.addContent(h2);
		} else {
			Element listInfo = new Element("h3");
			listInfo.setText("user list");
			body.addContent(listInfo);
			body.addContent(ul);
		}
		Element timeH2 = new Element("h2");
		if (Sender.launchTime.equals("0")) {
			timeH2.setText("Server is close.");
		} else {
			timeH2.setText("Server boot time: " + Sender.launchTime);
		}
		body.addContent(timeH2);
		xmlOut.output(html, out);
	}
}
